{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "66d5e925-905f-4364-ab72-842d9f18f167",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 7.倾斜开关实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：7.倾斜开关实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：倾斜开关实验\n",
    "倾斜开关实验，通过GPIOZero库的Button库直接检测倾斜传感器的状态，从而来点亮双色LED灯！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5f58331-df2f-409c-b08b-1401a5100a80",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "24bb5df6-9701-49a1-8df4-dcbd1a87c5fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpiozero import Button\n",
    "from gpiozero import LED\n",
    "from time import sleep\n",
    "from signal import pause"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf81ed90-ea51-4447-b522-85494d089b54",
   "metadata": {},
   "source": [
    "## 2.定义倾斜开关管脚和双色LED管脚"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "20561d89-e334-41e0-9358-07f9b385ffdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "makerobo_TiltPin = Button(17)     # 轻触倾斜传感器\n",
    "p_R = LED(18)        # 红色LED管脚\n",
    "p_G = LED(27)        # 绿色LED管脚"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "948171fb-35a4-4e99-a671-172c4c27a411",
   "metadata": {},
   "source": [
    "## 3.初始化工作及中断函数定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a0c58259-8f1b-48ee-beed-cd91d6cef020",
   "metadata": {},
   "outputs": [],
   "source": [
    "def pressed():\n",
    "    print('********************************') \n",
    "    print('* makerobo Raspberry Kit Tilt!!*') \n",
    "    print('********************************')\n",
    "    p_R.on()  # 打开红色LED\n",
    "    p_G.off() # 关闭绿色LED\n",
    "\n",
    "def released():\n",
    "    p_R.off()   # 关闭红色LED\n",
    "    p_G.on()    # 打开绿色LED\n",
    "\n",
    "makerobo_TiltPin.when_pressed = pressed\n",
    "makerobo_TiltPin.when_released = released"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04b01200-b8f4-4906-9fef-ee5921375822",
   "metadata": {},
   "source": [
    "## 4.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45e18609-d482-4008-9b7f-64ee3599f522",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************************\n",
      "* makerobo Raspberry Kit Tilt!!*\n",
      "********************************\n",
      "********************************\n",
      "* makerobo Raspberry Kit Tilt!!*\n",
      "********************************\n",
      "********************************\n",
      "* makerobo Raspberry Kit Tilt!!*\n",
      "********************************\n",
      "********************************\n",
      "* makerobo Raspberry Kit Tilt!!*\n",
      "********************************\n",
      "********************************\n",
      "* makerobo Raspberry Kit Tilt!!*\n",
      "********************************\n"
     ]
    }
   ],
   "source": [
    "# 循环函数\t\n",
    "def makerobo_loop():\n",
    "    pause()\n",
    "\n",
    "# 释放资源\n",
    "def makerobo_destroy():\n",
    "    p_R.close()\n",
    "    p_G.close()\n",
    "\n",
    "# 程序入口\n",
    "if __name__ == '__main__':\n",
    "\ttry:\n",
    "\t\tmakerobo_loop()        #  调用循环函数\n",
    "\texcept KeyboardInterrupt:  #  当按下Ctrl+C时，将执行destroy()子程序。\n",
    "\t\tmakerobo_destroy()     #  释放资源"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
